<?php
   class BusinessAction extends CommonAction{
   	public function _filter(&$map)
   	{
   		$map['removed']=1;//1为未删除，2为回收站
   		$companyname=$_REQUEST['companyname'];
   		$userid=$_REQUEST['userid'];
   		$start=$_REQUEST['start'];
   		$end=$_REQUEST['end'];
   		if(!empty($companyname))
   		{
   			$map['companyname']=array('like','%'.$companyname.'%');
   		}
//    		if(!empty($userid))
//    		{
//    			$map['userid']=array('eq',$userid);
//    		}
      /*   if('1'!==$_SESSION [C ( 'USER_AUTH_KEY' )]){
        	$map['userid']=$_SESSION [C ( 'USER_AUTH_KEY' )];
        }
   		 */
   		if(!empty($start)||!empty($end))
   		{
   			$map['businessdate']=array('between',array($start,$end));
   		}
   	}
	   	public function _before_index() {
	   		//默认管理员的id为1
	   		$userid=$_SESSION[C('USER_AUTH_KEY')];
	   		// $role=D('Role');
	   		/* if($userid=='1')
	   		{
	   			$this->assign('flag','isAdmin');
	   		} */
	   		//判断是否有业务的写权限
// 	   		$bs=$this->isBusiness($userid);
// 	   		if($bs||$userid==1)
// 	   		{
	   			$this->assign('flag','flag');
	   		//}
	   		
	   		
// 	   		$model = M ( "Business" );
// 	   		$list = $model->select();
// 	   		$this->assign ( 'list', $list );
            
	   			//获得用户信息
	   			$umodel=M('User');
	   			$users=$umodel->select();
	   			
	   	   $this->assign('us',$users);
	   		$this->assign('user',$userid);
	   	}
	   	public function _before_add() {
	   		$userid=$_SESSION[C('USER_AUTH_KEY')];
	   		//$role=D('Role');
	   		//管理员的id默认为1
	   		//获得是否具有写权限的
	   		$bs=$this->isBusiness($userid);
	   		//有写权限同时又不是管理员的
	   		if($bs&&$userid!=1)
	   		{
	   			$this->assign('flag','isbusiness');
	   		}
	   		$model = M ( "Business" );
	   		$list = $model->select ();
	   		$this->assign ( 'list', $list );
	   	}
	   	public function add(){
	   		if("true"==$_REQUEST['getgoods']){
	   			$this->getgoods();
	   		}else{
	   			if("true"==$_REQUEST['insert']){
	   				$this->insert();
	   			}else{
	   				$this->display('add');
	   			}
	   		}
	   	}
	   	public function _before_edit() {
	   		$model = M ( "Business" );
	   		$list = $model->select ();
	   		$this->assign ( 'list', $list );
	   		$id = $_REQUEST ['id'];
	   		$bg = M('BusinessGood');
	   		$con = $bg->where('bussinessid='.$id)->select();
	   		$this->assign('bg',$con);
	   	} 
	   	
	   	
	   	public function _before_update()
	   	{
	   		$id = $_REQUEST ['id'];
	   		$this->AddMerchandise($id);
	   		
	   		
	   	}

	   	public function read()
	   	{
	   		$model = M ( "Business" );
	   		$id=$_REQUEST['id'];
	   		$viewid=$_SESSION[C('USER_AUTH_KEY')];
	   		$viewname=$_SESSION['username'];
	   		$list=$model->where('id='.$id)->find();
	   		$uid=$list['userid'];
	   		$u=M('User');
	   		$uname=$u->where('id='.$uid)->getField('username');
	   		$vid=$list['viewids'];
	   		$vname=$list['viewnames'];
	   		$data=array();
	   		if($viewid!=$uid){
	   		if(!empty($vid))
	   		{
	   			$data['viewids']=$vid.','.$viewid;
	   		}
	   		else 
	   		{
	   			$data['viewids']=$viewid;
	   		}
	   		if(!empty($vname))
	   		{
	   			$data['viewnames']=$vname.','.$viewname;
	   		}
	   		else
	   		{
	   			$data['viewnames']=$viewname;
	   		}
	   		}
	   		//$data['viewtime']=date('Y-m-d');
	   		
	   		$model->data($data)->where('id='.$id)->save();
	   		$this->assign('uname',$uname);
	   		$result=$model->where('id='.$id)->find();
	   		$this->assign("vo",$result);
	   		$models=M('BusinessGood');
	   		$list=$models->where('bussinessid='.$id)->select();
	   		$this->assign('bg',$list);
	   		$this->display();	
	   	}
	   	
	   	public function goodsedit()
	   	{
	   		$id=$_REQUEST['id'];
	   		$model=M('BusinessGood');
	   		$list=$model->where('id='.$id)->find();
	   		$this->assign('vo',$list);
	   		$this->display();
	   		
	   	}
	   	//拟定合同
	   	public function draft()
	   	{
	   		//业务的id
	   		$id=$_REQUEST['id'];
	   		//跳转到合同添加的页面
	   		$this->redirect('Contract/add','id='.$id);
	   		//$this->ajaxReturn($id,'跳转',200);
	   		
	   	}
	   	
	   	//判断是不是具有写权限
	   	public function isBusiness($id)
	   	{
	   		$model=D('Business');
	   		//$table=$model->tablePrefix.'access';
	   		$rs=$model->query('select * from mlt_access a ,mlt_role_user b where node_id=122 and pid=85 and level=3 and a.role_id=b.role_id and b.user_id='.$id);
	   		if($rs==true)
	   		{
	   			return true;
	   		}
	   		else
	   		{
	   			return false;
	   		}
	   	}
	   	
	   	
	   	/*   //判断是否是管理员
	   	 public function isAdmin()
	   	 {
	   	$table = $this->tablePrefix.'access';
	   	$rs = $this->db->query('select * from '.$table.' as a, '.$this->tablePrefix.'role_user as b  where user_id=1 and a.role_id=b.role_id ');
	   	if($rs==false)
	   	{
	   	return false;
	   	}
	   	else
	   	{
	   	return true;
	   	}
	   	} */
        //高级检索
	   	public function retrieval(){
	   		$model=M('User');
	   		$user=$model->select();
	   		$this->assign('user',$user);
	   		$this->display();
	   	}
	   //导出功能
   public function export(){
   	
	   	$model=D('Business');
	   	$userid=$_REQUEST['userid'];
	   	$start=$_REQUEST['start'];
	   	$end=$_REQUEST['end'];
	   	$sql='select b.id  id, companyname,linkname,b.phone,b.address,fullname from mlt_business b  ,mlt_user u  where b.userid=u.id and b.removed=1 ';
	    if(!empty($userid)){
	        	$sql.=' and b.userid='.$userid;
	    }
	    if(!empty($start)||!empty($end)){
	        	$sql.=' and b.businessdate between \''.$start.'\' and \''.$end.'\'';
	        }
	    $sql.=' and b.removed!= 2';
	  	$business=$model->query($sql);
	   	if(!empty($business))
	  	{
	  		for ($i=0;$i<count($business);$i++)
	  		{
	  			$id=$business[$i]['id'];
	  			$buslst[$i]['companyname']=$business[$i]['companyname'];
	  			$buslst[$i]['linkname']=$business[$i]['linkname'];
	  			$buslst[$i]['phone']=$business[$i]['phone'];
	  			$buslst[$i]['productname']=$this->getGoodsname($id);
	  			$buslst[$i]['totalprice']=$this->getTotalPrice($id);
	  			$buslst[$i]['address']=$business[$i]['address'];
	  			$buslst[$i]['fullname']=$business[$i]['fullname'];
	  			
	  		}
	  	}   	 
	   	$cname=date('Y-m-d');
	   	
	   	$map = array('公司名称','联系人','电话','购买产品名称','金额','地址','操作人');
	   	$field=array('companyname','linkname','phone','productname','totalprice','address','fullname');
	   	
	   	$this->phpexcel($buslst, $cname, $map,$field);
	   	
	   } 
	   //获得商品的总金额
	   public function getTotalPrice($id)
	   {
	     $bg=M('BusinessGood');
	   	 $list=$bg->query('select sum(totalprice) totalprice from mlt_business_good where bussinessid='.$id);
	   	 if(!empty($list))
	   	 {
	   	 	$result=$list[0]['totalprice'];
	   	 	if(empty($result))
	   	 	{
	   	 		//return 0;
	   	 		$result=0;
	   	 	}
	   	 	return $result;
	   	 } 
	    return 0;
	   
	   }
	   //获得商品名称
	   public function getGoodsname($id)
	   {
	   	 $bg=M('BusinessGood');
	   	 $list=$bg->where('bussinessid='.$id)->select();
	   	 if(!empty($list))
	   	 {
	   	 	for ($i = 0;  $i<count($list); $i++) {
	   	 		$result=$result.$list[$i]['goodname'].',';
	   	 	}
	   	 	$result=substr($result,0,strlen($result)-1);
	   	 }
	   	 return $result;
	   }
	   //显示回收站中的记录
	  /*  public function trash(){
		   	$model=M('Business');
		   	import ( '@.Org.Page' );
		   	$count = $model->where('removed=2')->count ();
		   	$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
		   	$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		   	$list = $model->where('removed=2')->page ( $pageNum . "," . $page->listRows )->order('id desc')->select ();
		   	$this->assign('list',$list);
		   	$this->assign ( 'totalCount', $count ); // 总条数
		   	$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		   	$this->assign ( 'currentPage', $pageNum ); // 当前页
		   	$this->display();
	   }
	   public function restore(){
	   	    $id=$_REQUEST['id'];
	   	    $model=M('Business');
	   	    $result=$model->where('id='.$id)->setField('removed','1');
	   	    if($result!==false){
	   	    	$this->ajaxReturn (1,'还原成功!',200);
	   	    }else{
	   	    	$this->ajaxReturn (1,'还原失败!',300);
	   	    }
	   	    
	   } */
	   
	   
	   public function goodsupdate()
	   {
	   	$id=$_REQUEST['id'];
	   	$model=M('BusinessGood');
	   	$data['goodname']=$_REQUEST['goodname'];
	   	$data['quality']=$_REQUEST['quality'];
	   	$data['price']=$_REQUEST['price'];
	   	$data['totalprice']=$_REQUEST['totalprice'];
	   	$list=$model->where('id='.$id)->find();
	   	
	   	$result=$model->where('id='.$id)->save($data);
	   	if($result)
	   	{
	   		$this->ajaxReturn (1,'更新成功!',200);
	   		//$this->redirect('edit','id='.$list['bussinessid']);
	   	}
	   	else
	   	{
	   		$this->ajaxReturn (1,'新增失败!',300);
	   	}
	   	
	   }
	   
	   public function insert() {
	   	
	   	$name=$this->getActionName();
	   	$model = D ($name);
	   	if (false === $model->create ()) {
	   		$this->ajaxReturn(1, $model->getError () ,300);
	   	}
	   	$info=$this->getType($name);
	   
	   	//保存当前数据对象
	   	$list=$model->add ();
	   	if ($list!==false) { //保存成功
	   		//保存商品信息包括种类，吨数，单价，总额
	   		$this->AddMerchandise($list);
	   		$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
	   		$this->insertLog('添加','操作'.$info.'编号为'.$list.'的数据成功');
	   		$this->ajaxReturn (1,'新增成功!',200);
	   	} else {
	   		//失败提示
	   		$this->insertLog('添加','操作'.$info.'编号为'.$list.'的数据失败');
	   		$this->ajaxReturn (1,'新增失败!',300);
	   	}
	   
	   
	   }
	   
	   /**
	    * 保存商品信息包括种类，吨数，单价，总额
	    */
	   public function AddMerchandise($list){
	   	$model = M('BusinessGood');
	   	$goodname = $_POST['items_org_goodname'];
	   	$quality = $_POST['items_itemQuality'];
	   	$price = $_POST['items_itemPrice'];
	   	$totalprice = $_POST['items_itemTotalprice'];
	   	if(count($goodname)>=0){
	   		for($i=0 ; $i < count($goodname);$i++){
	   			$data['bussinessid']=$list;
	   			$data['goodname']=$goodname[$i];
	   			$data['quality']=$quality[$i];
	   			$data['price']=$price[$i];
	   			$data['totalprice']=$totalprice[$i];
	   			$model->add($data);
	   		}
	   	}
	   }
	   
	   public function getgoods()
	   {
	   	$goods=M('Goods');
	   	$list=$goods->select();
	   	$this->assign ( 'list', $list );
	   	$this->display ("getgoods");
	   
	   }
	   
	   public function goodsdel()
	   {
	   	$id=$_REQUEST['id'];
	   	$model=M('BusinessGood');
	   	$result=$model->where('id='.$id)->delete();
	   	if($result)
	   	{
	   		$this->ajaxReturn (1,'删除成功!',200);
	   	}
	   	else
	   	{
	   		$this->ajaxReturn (1,'删除失败!',300);
	   	}
	   	
	   }
	   
	   
	   public function _before_foreverdelete()
	   {
	   	$id = $_REQUEST ['ids'];
	   	$model=M('BusinessGood');
	   	$pk='bussinessid';
	   	if (isset ( $id )) {
	   		$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
	   		if (false !== $model->where ( $condition )->delete ()) {
	   			//$this->ajaxReturn (1,'删除成功!',200);
	   		} else {
	   			//$this->insertLog('彻底删除','操作'.$info.'编号为'.$id.'的数据失败');
	   			$this->ajaxReturn (1,'删除失败!',300);
	   		}
	   	} else {
	   		$this->ajaxReturn (1,'进行删除操作出现非法操作!',200);
	   	}
	   	
	   }
   
   }
?>